package com.yuyue.springFramework.note17;

import com.yuyue.springFramework.note17.examp2.AnnoPersonService1;
import com.yuyue.springFramework.note17.examp2.MyConfig;
import com.yuyue.springFramework.note17.examp2.Person;
import com.yuyue.springFramework.note17.example.service.IPersonService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 《Spring-17 声明式事务》文章代码
 *  文章地址：
 */
public class Note17Test {
    @Test
    //XML方式配置
    public void fun1(){
        //获取容器
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext08.xml");

        //获取持久化对象
        Person person = applicationContext.getBean("person", Person.class);
        //有接口的情况下，此处应使用接口获取，而不能使用实现类，否则会因为类型错误导致报错
        //PersonService personService = applicationContext.getBean("personService", PersonService.class);
        IPersonService personService = applicationContext.getBean("personService", IPersonService.class);

        person.setId("13");
        person.setName("张楚岚");
        person.setAge(21);
        person.setSex("男");

        personService.testPerson(person);
    }

    @Test
    //同时验证不符合方法名称和public修饰时，无事务
    public void fun2(){
        //获取容器
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext08.xml");

        //获取持久化对象
        Person person = applicationContext.getBean("person", Person.class);
        //有接口的情况下，此处应使用接口获取，而不能使用实现类，否则会因为类型错误导致报错
        //PersonService personService = applicationContext.getBean("personService", PersonService.class);
        IPersonService personService = applicationContext.getBean("personService", IPersonService.class);

        person.setId("14");
        person.setName("王道长");
        person.setAge(21);
        person.setSex("男");

        //public修饰，用于调用带事务非public方法
        personService.NotTestStart(person);
    }

    @Test
    //注解风格事务控制
    public void fun3(){
        //获取容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(MyConfig.class);
        applicationContext.refresh();

        //填充数据库对应对象
        Person person = applicationContext.getBean("person", Person.class);
        person.setName("冯宝宝");
        person.setSex("女");
        person.setAge(100);
        person.setId("15");

        AnnoPersonService1 personService1 = applicationContext.getBean("annoPersonService1", AnnoPersonService1.class);
        try {
            //带事务方法：插入并报错
            personService1.test1(person);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }

        //查询插入结果
        System.out.println("事务回滚，因此查询结果为空：" + personService1.queryOneObjectList(person.getId()));
    }

    @Test
    //修饰符非public
    public void fun4(){
        //获取容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(MyConfig.class);
        applicationContext.refresh();

        //填充数据库对应对象
        Person person = applicationContext.getBean("person", Person.class);
        person.setName("冯宝宝");
        person.setSex("女");
        person.setAge(100);
        person.setId("15");

        AnnoPersonService1 personService1 = applicationContext.getBean("annoPersonService1", AnnoPersonService1.class);
        try {
            //public修饰，用于调用带事务非public方法
            personService1.test3(person);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }

        //查询插入结果
        System.out.println("事务未回滚，插入成功：" + personService1.queryOneObjectList(person.getId()));
    }
}
